package com.ibeeking.gateway.cp.constant;

import com.ibeeking.nematos.constants.enums.LogClassifyEnum;
import com.ibeeking.nematos.constants.enums.LogConstant;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志跟踪工具类
 *
 * @Author ibeeking
 */

public class LogUtils {
    private static final Logger log = LoggerFactory.getLogger(getClassName());

    private LogUtils() {
        throw new IllegalStateException("参数异常");
    }

    public static void debug(String msg) {
        if (log.isDebugEnabled()) {
            log.debug(getFormat(LogClassifyEnum.TRACE_LOG, msg));
        }
    }

    public static void debug(String msg, Object... var2) {
        if (log.isDebugEnabled()) {
            log.debug(getFormat(LogClassifyEnum.TRACE_LOG, msg), var2);
        }
    }

    /**
     * @param msg 日志标题
     */
    public static void debug(LogClassifyEnum logClassifyEnum, String msg) {
        if (log.isDebugEnabled()) {
            log.debug(getFormat(logClassifyEnum, msg));
        }
    }

    public static void debug(LogClassifyEnum logClassifyEnum, String msg, Object... var2) {
        if (log.isDebugEnabled()) {
            log.debug(getFormat(logClassifyEnum, msg), var2);
        }
    }

    public static void info(String msg) {
        if (log.isInfoEnabled()) {
            log.info(getFormat(LogClassifyEnum.TRACE_LOG, msg));
        }
    }

    public static void info(String msg, Object... var2) {
        if (log.isInfoEnabled()) {
            log.info(getFormat(LogClassifyEnum.TRACE_LOG, msg), var2);
        }
    }

    public static void info(LogClassifyEnum logClassifyEnum, String msg) {
        if (log.isInfoEnabled()) {
            log.info(getFormat(logClassifyEnum, msg));
        }
    }

    public static void info(LogClassifyEnum logClassifyEnum, String msg, Object... var2) {
        if (log.isInfoEnabled()) {
            log.info(getFormat(logClassifyEnum, msg), var2);
        }
    }

    public static void warn(String msg) {
        if (log.isWarnEnabled()) {
            log.warn(getFormat(LogClassifyEnum.TRACE_LOG, msg));
        }
    }

    public static void warn(String msg, Object... var2) {
        if (log.isWarnEnabled()) {
            log.warn(getFormat(LogClassifyEnum.TRACE_LOG, msg), var2);
        }
    }

    public static void warn(LogClassifyEnum logClassifyEnum, String msg) {
        if (log.isWarnEnabled()) {
            log.warn(getFormat(logClassifyEnum, msg));
        }
    }

    public static void warn(LogClassifyEnum logClassifyEnum, String msg, Object... var2) {
        if (log.isWarnEnabled()) {
            log.warn(getFormat(logClassifyEnum, msg), var2);
        }
    }

    public static void error(String msg) {
        if (log.isErrorEnabled()) {
            log.error(getFormat(LogClassifyEnum.TRACE_LOG, msg));
        }
    }

    public static void error(String msg, Object... var2) {
        if (log.isErrorEnabled()) {
            log.error(getFormat(LogClassifyEnum.TRACE_LOG, msg), var2);
        }
    }

    public static void error(LogClassifyEnum logClassifyEnum, String msg) {
        if (log.isErrorEnabled()) {
            log.error(getFormat(logClassifyEnum, msg));
        }
    }

    public static void error(LogClassifyEnum logClassifyEnum, String msg, Object... var2) {
        if (log.isInfoEnabled()) {
            log.error(getFormat(logClassifyEnum, msg), var2);
        }
    }

    /**
     * 格式为：{时间}|{应用名}|{级别}|{链路id}|{线程名}|{类全路径}|{租户id}|{操作人}|
     * {日志分类}|{描述}|{数据体。（json）}
     *
     * @param describe 描述
     * @return
     */
    private static String getFormat(LogClassifyEnum logClassifyEnum, String describe) {
        return logClassifyEnum.getValue() + LogConstant.SPLIT + describe;
    }

    /**
     * 请求跟踪
     *
     * @param reqIp      请求ip
     * @param reqSource  请求来源，操作系统&浏览器及版本
     * @param describe   描述
     * @param resources  请求资源信息 请求方式 +请求全路径
     * @param reqData    请求数据体
     * @param resData    响应数据体
     * @param takeUpTime 耗时
     */
    public static void reqTrace(String reqIp, String reqSource, String describe, String resources, String reqData, String resData, Long takeUpTime) {
        if (log.isDebugEnabled()) {
            log.debug(getOperatorFormat(OperateLogTypeEnum.UNKNOWN, reqIp, reqSource, describe, resources, reqData, resData, takeUpTime));
        }
    }


    /**
     * 格式为：{时间}|{应用名}|{级别}|{链路id}|{线程名}|{类全路径}|{租户id}|{操作人}|
     * {日志分类}|{操作类型}|{请求ip}|{请求来源信息}|{描述}|{请求资源信息}|{数据体。（json）}
     *
     * @param operateLogTypeEnum 操作类型
     * @param reqIp              请求ip
     * @param reqSource          请求来源，操作系统&浏览器及版本
     * @param describe           描述
     * @param resources          请求资源信息 请求方式 +请求全路径
     * @param reqData            请求数据体
     * @param resData            响应数据体
     * @param takeUpTime         耗时
     * @return
     */
    private static String getOperatorFormat(
            OperateLogTypeEnum operateLogTypeEnum,
            String reqIp,
            String reqSource,
            String describe,
            String resources,
            String reqData,
            String resData,
            Long takeUpTime) {
        return LogClassifyEnum.OPERATION_LOG.getValue()
                + LogConstant.SPLIT + operateLogTypeEnum.getValue()
                + LogConstant.SPLIT + reqIp
                + LogConstant.SPLIT + reqSource
                + LogConstant.SPLIT + describe
                + LogConstant.SPLIT + resources
                + LogConstant.SPLIT + reqData
                + LogConstant.SPLIT + resData
                + LogConstant.SPLIT + takeUpTime
                ;
    }

    private static String getClassName() {
        return new SecurityManager() {
            public String getClassName() {
                return getClassContext()[3].getName();
            }
        }.getClassName();
    }
}
